package com.craftplay.dao;

import com.craftplay.model.User;
import com.craftplay.model.UserKey;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface UserMapper {
    @Delete({
            "delete from user",
            "where user_id = #{userId,jdbcType=INTEGER}",
            "and open_id = #{openId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(UserKey key);

    @Insert({
            "insert into user (user_id, open_id, ",
            "nickname, sex, province, ",
            "city, country, head_url, ",
            "unionid, feature, ",
            "intro, intro_img_url, ",
            "place, place_img_url, ",
            "user_type,",
            "tel,subscribe)",
            "values (#{userId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, ",
            "#{nickname,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{province,jdbcType=VARCHAR}, ",
            "#{city,jdbcType=VARCHAR}, #{country,jdbcType=VARCHAR}, #{headUrl,jdbcType=VARCHAR}, ",
            "#{unionid,jdbcType=VARCHAR}, #{feature,jdbcType=VARCHAR}, ",
            "#{intro,jdbcType=VARCHAR}, #{introImgUrl,jdbcType=VARCHAR}, ",
            "#{place,jdbcType=VARCHAR}, #{placeImgUrl,jdbcType=VARCHAR}, ",
            "#{userType,jdbcType=INTEGER}, ",
            "#{tel,jdbcType=VARCHAR}, #{subscribe,jdbcType=INTEGER})"
    })
    int insert(User record);

    @InsertProvider(type = UserSqlProvider.class, method = "insertSelective")
    int insertSelective(User record);

    @Select({
            "select",
            "user_id, open_id, nickname, sex, province, city, country, head_url, unionid, ",
            "feature, intro, intro_img_url, place, place_img_url, tel ,subscribe,user_type",
            "from user",
            "where user_id = #{userId,jdbcType=INTEGER}"

    })
    @Results({
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "open_id", property = "openId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "nickname", property = "nickname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sex", property = "sex", jdbcType = JdbcType.INTEGER),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "country", property = "country", jdbcType = JdbcType.VARCHAR),
            @Result(column = "head_url", property = "headUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "unionid", property = "unionid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "feature", property = "feature", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro", property = "intro", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro_img_url", property = "introImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place", property = "place", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place_img_url", property = "placeImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tel", property = "tel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "subscribe", property = "subscribe", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_type", property = "userType", jdbcType = JdbcType.INTEGER)
    })
    User selectByPrimaryKey(Integer userId);


    @Select({
            "select",
            "user_id, open_id, nickname, sex, province, city, country, head_url, unionid, ",
            "feature, intro, intro_img_url, place, place_img_url, tel ,subscribe,user_type",
            "from user",
            "where open_id = #{openId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "open_id", property = "openId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "nickname", property = "nickname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sex", property = "sex", jdbcType = JdbcType.INTEGER),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "country", property = "country", jdbcType = JdbcType.VARCHAR),
            @Result(column = "head_url", property = "headUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "unionid", property = "unionid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "feature", property = "feature", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro", property = "intro", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro_img_url", property = "introImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place", property = "place", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place_img_url", property = "placeImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tel", property = "tel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "subscribe", property = "subscribe", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_type", property = "userType", jdbcType = JdbcType.INTEGER)

    })
    User selectByOpenId(String openId);

    @Select({
            "select",
            "intro",
            "from user",
            "where user_id = #{userId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER, id = true),
    })
    String getUserIntroByOwnerId(int userId);


    @UpdateProvider(type = UserSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    //查询满足某个条件的用户
    @Select({"<script>",
            "select",
            "user_id, open_id, nickname, sex, province, city, country, head_url, unionid, ",
            "feature, intro, intro_img_url, place, place_img_url, tel ,subscribe,user_type",
            "from user",
            "where 1=1",
            "<if  test='nickname!=null'>",
            " and nickname like '%${nickname}%'",
            "</if>",
            "<if  test='userType!=null'>",
            " and user_type = #{userType}",
            "</if>",
            "</script>"
    })
    @Results({
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "open_id", property = "openId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nickname", property = "nickname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sex", property = "sex", jdbcType = JdbcType.INTEGER),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "country", property = "country", jdbcType = JdbcType.VARCHAR),
            @Result(column = "head_url", property = "headUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "unionid", property = "unionid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "feature", property = "feature", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro", property = "intro", jdbcType = JdbcType.VARCHAR),
            @Result(column = "intro_img_url", property = "introImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place", property = "place", jdbcType = JdbcType.VARCHAR),
            @Result(column = "place_img_url", property = "placeImgUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tel", property = "tel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "subscribe", property = "subscribe", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_type", property = "userType", jdbcType = JdbcType.INTEGER)
    })
    List<User> selectAllUserByColumn(User user);


    @Update({
            "update user",
            "set nickname = #{nickname,jdbcType=VARCHAR},",
            "sex = #{sex,jdbcType=INTEGER},",
            "province = #{province,jdbcType=VARCHAR},",
            "city = #{city,jdbcType=VARCHAR},",
            "country = #{country,jdbcType=VARCHAR},",
            "head_url = #{headUrl,jdbcType=VARCHAR},",
            "unionid = #{unionid,jdbcType=VARCHAR},",
            "feature = #{feature,jdbcType=VARCHAR},",
            "intro = #{intro,jdbcType=VARCHAR},",
            "intro_img_url = #{introImgUrl,jdbcType=VARCHAR},",
            "place = #{place,jdbcType=VARCHAR},",
            "place_img_url = #{placeImgUrl,jdbcType=VARCHAR},",
            "tel = #{tel,jdbcType=VARCHAR},",
            "user_type = #{userType,jdbcType=INTEGER},",
            "subscribe = #{subscribe,jdbcType=INTEGER}",
            "where user_id = #{userId,jdbcType=INTEGER}",
            "and open_id = #{openId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(User record);


    @Update({
            "update user",
            "set subscribe = #{subscribe,jdbcType=INTEGER}",
            "where open_id = #{openId,jdbcType=VARCHAR}"
    })
    int updateSubscribeByOpenId(@Param("openId") String openId, @Param("subscribe") int subscribe);


    @Update({
            "update user",
            "set feature = #{feature,jdbcType=VARCHAR},",
            "tel = #{tel,jdbcType=VARCHAR},",
            "user_type = #{userType,jdbcType=INTEGER},",
            "nickname = #{nickname,jdbcType=VARCHAR}",
            "where open_id = #{openId,jdbcType=VARCHAR}"
    })
    int updateUserInfo(@Param("feature") String feature,
                       @Param("tel") String tel, @Param("openId") String openId, @Param("nickname") String nickname, @Param("userType") Integer userType);

    @Update({
            "update user",
            "set intro = #{intro,jdbcType=VARCHAR},",
            "intro_img_url = #{introImgUrl,jdbcType=VARCHAR}",
            "where open_id = #{openId,jdbcType=VARCHAR}"
    })
    int updateIntro(@Param("intro") String feature,
                    @Param("introImgUrl") String introImgUrl, @Param("openId") String openId);


    @Update({
            "update user",
            "set place = #{place,jdbcType=VARCHAR},",
            "place_img_url = #{placeImgUrl,jdbcType=VARCHAR}",
            "where open_id = #{openId,jdbcType=VARCHAR}"
    })
    int updatePlaceInfo(@Param("place") String place, @Param("placeImgUrl") String placeImgUrl, @Param("openId") String openId);

    @Select("select * from user where user_type=1  and user_id not in (select focusUserId from  focus WHERE userId = #{userId})")
    List<User> findTeacher(@Param(value = "userId") Integer userId);
}